package org.firebug.spring.boot.apollo.controller;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.firebug.spring.boot.apollo.properties.AuthorProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping(value = {"/apollo"})
public class ApolloController {
    private Config config = ConfigService.getAppConfig();

    @Value(value = "${author.name}")
    private String name;

    @Resource
    private AuthorProperties authorProperties;

    /**
     *   实时更新：ConfigService.getAppConfig().getProperty()
     *   实时更新：@Value(value = "${author.name}")
     * 非实时更新：@ConfigurationProperties(prefix = "author")
     */
    @RequestMapping(value = {"/author/name"})
    public Map<String, String> jdbc() {
        String key = "author.name";
        Map<String, String> map = new LinkedHashMap<>();
        map.put("ConfigService.getAppConfig().getProperty()", config.getProperty(key, ""));
        map.put("@Value", this.name);
        map.put("authorProperties", authorProperties.getName());
        return map;
    }
}
